package com.bw.day04zh

import androidx.lifecycle.viewModelScope
import com.bw.base.entity.CodeSendEntity
import com.bw.base.entity.Res
import com.bw.base.mvvm.BaseViewModel
import com.bw.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class MainViewModel:BaseViewModel() {
    //使用协程当时发送验证码功能   ---数据倒灌
    val success = SingleLiveData<Res<CodeSendEntity>>()
    val fail = SingleLiveData<String>()
    fun codeSend(phone:String){
        viewModelScope.launch {
            //自己添加try-catch方法，接收访问失败的情况
            try {
                success.postValue(apiService.codeSend(phone))
            }catch (e:Exception){
                fail.postValue(e.message.toString())
            }
        }
    }
}